package com.cn.http.service.impl;

import cn.com.allunion.sms.pojo.Smsrecord;
import com.alibaba.fastjson.JSON;
import com.cn.http.service.AbstractSmsSendService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 骏逸(移动)短信发送服务
 * Created by YangJie on 2016/4/21.
 */
public class JunYiMobileSmsSendService extends AbstractSmsSendService {
    private static final Logger LOGGER = LoggerFactory.getLogger(JunYiMobileSmsSendService.class) ;

    private final String smsDefaultSign ;

    public JunYiMobileSmsSendService(String smsDefaultSign) {
        this.smsDefaultSign = smsDefaultSign;
    }

    @Override
    public String send(List<Smsrecord> smsrecords) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("---> 骏逸(移动)短信发送服务 开始发送短信");
        }
//        LOGGER.debug("junyi:item" + item);
//        LOGGER.debug("junyi:sign" + sign);
        String result = null;

        if (smsrecords == null || smsrecords.size() == 0) {
            return "sms记录为空";
        }
        /**
         * 后加的群发功能，为了不影响单发，单独判断群发
         */
//		if (smsRecos.size() != 1) {
//			String smsOperator = "";
//			String operatorMessageID = "";
//			String sendContent = "";
//			String sendNumber = item;
//			LOGGER.debug("短信群发电话号码处理前:" + sendNumber);
//			LOGGER.debug("短信群发电话号码处理后为:"
//					+ dealTels(HttpClients.QXTparam, sendNumber,
//							HttpClients.QXTsepar));
//			String sendState = "";
//			StringBuffer smsRecordID = new StringBuffer();
//			String businessRecord = "";
//
//			for (SMSRecord smsRecord2 : smsRecos) {
//				String res = recordAction.addNewRecord(smsRecord2);
//				LOGGER.debug(smsRecord2.getSmsRecordID());
//				LOGGER.debug("res=" + res);
//				if (res.equals("success")) {
//					smsOperator = smsRecord2.getSmsOperator().getJoinNum();
//
//					operatorMessageID = smsRecord2.getOperatorMessageID();
//					sendContent = smsRecord2.getSendContent();
//					sendState = smsRecord2.getSendState();
//					businessRecord = smsRecord2.getBusinessRecord()
//							.getBusinessCode();
//
//					if (smsRecordID.length() == 0) {
//						LOGGER.debug("res=" + 1);
//						smsRecordID.append(smsRecord2.getSmsRecordID());
//					} else {
//						LOGGER.debug("res=" + 2);
//						smsRecordID.append(HttpClients.QXTsepar
//								+ smsRecord2.getSmsRecordID());
//					}
//
//					result = "接收成功！";
//				} else {
//					result = "发送之前存储失败";
//					LOGGER.debug("发送之前存储失败");
//				}
//			}
//			String mess = "{'operating':'null','smsOperator':" + "'"
//					+ smsOperator + "'" + ",'operatorMessageID':" + "'"
//					+ operatorMessageID + "'" + ",'sendContent':" + "'"
//					+ sendContent + "'" + ",'sendNumber':" + "'" + sendNumber
//					+ "'" + ",'sendState':" + "'" + sendState + "'"
//					+ ",'smsRecordID':" + "'" + smsRecordID.toString() + "'"
//					+ ",'businessRecord':" + "'" + businessRecord + "'"
//					+ ",'sign':" + "'【" + sign + "】'" + "}";
//			LOGGER.debug("群发短信前json为:" + mess);
//			sendSMS.send(mess);
//
//		} else {
        /**
         * 单发短信功能
         */
        for (Smsrecord smsRecord2 : smsrecords) {
            String res = smsrecordService.addNewRecord(smsRecord2);
//            String sendNumber = smsRecord2.getSendNumber();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("短信记录ID： {}", smsRecord2.getSmsRecordID());
                LOGGER.debug("短信单发电话号码处理前: {}", smsRecord2.getSendNumber());
                LOGGER.debug("骏逸号码不加86");
            }

            if (res.equals("success")) {

//                String mess = "{'operating':'null','smsOperator':" + "'"
//                        + smsRecord2.getSmsOperator().getJoinNum() + "'"
//                        + ",'operatorMessageID':" + "'"
//                        + smsRecord2.getOperatorMessageID() + "'"
//                        + ",'sendContent':" + "'"
//                        + smsRecord2.getSendContent() + "'"
//                        + ",'sendNumber':" + "'" + sendNumber + "'"
//                        + ",'sendState':" + "'" + smsRecord2.getSendState()
//                        + "'" + ",'smsRecordID':" + "'"
//                        + smsRecord2.getSmsRecordID() + "'"
//                        + ",'businessRecord':" + "'"
//                        + smsRecord2.getSmsBusinessRecord().getBusinessCode()
//                        + "'" + ",'sign':" + "'【" + smsDefaultSign + "】'" + "}";

                // 发送消息到消息队列
                String message = getSendMessage(smsRecord2, smsDefaultSign) ;
                if(LOGGER.isDebugEnabled()) {
                    LOGGER.debug("message： {}", message);
                }

                // 发送消息到消息队列
                if (StringUtils.isNotEmpty(message)) {
                    sendSMS.send(message);
                    result = "接收成功！";
                } else {
                    result = "接收失败，无法获取转换后的消息， smsrecord: " + JSON.toJSONString(smsRecord2);
                }
            } else {
                result = "发送之前存储失败";
                LOGGER.debug("发送之前存储失败");
            }
//			}

        }
        return result;
    }

    public String getSmsDefaultSign() {
        return smsDefaultSign;
    }
}
